package com.smap.control;

import com.smap.core.Control;
import com.smap.core.Map;
import com.smap.event.MapEvent;

public abstract class AbstractControl implements Control {

	Map map;
	ToolBar toolBar;
	String activeTool;
	public boolean isActive = false;
	String group = null;

	public void install(Map map) {
		this.map = map;
		toolBar = map.getToolBar();

		if (this.getCmd() != null)
			for (String cmd : this.getCmd()) {
				toolBar.addBtn(cmd);
			}
		this.init();
	}

	abstract void init();

	@Override
	public void activate() {
		isActive = true;
	}

	public void activate(String cmd) {
		activate();
		activeTool = cmd;
		execute(cmd);
	}

	abstract void execute(String cmd);

	@Override
	public void deActivate() {
		isActive = false;
		activeTool = null;
	}

	public void onMapEvent(MapEvent mapEvent) {
		if (isActive && !mapEvent.stop) {
			onEvent(mapEvent);
		}
	}

	@Override
	public boolean acceptCmd(String cmd) {
		if (this.getCmd() != null)
			for (String line : this.getCmd()) {
				if (line.equals(cmd)) {
					return true;
				}
			}
		return false;
	}

	public void onResize() {

	}

	abstract void onEvent(MapEvent evt);

	public String[] getCmd() {
		return new String[0];
	}

	@Override
	public String getGroup() {
		return group;
	}

	public void setGroup(String group) {
		this.group = group;
	}

	@Override
	public String getActive() {
		if (this.group != null) {
			return activeTool;
		} else {
			return null;
		}
	}

}
